<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 29/08/2017
 * Time: 13:37
 */
namespace frontend\controllers;

use common\models\ar\HistoryLogin;
use frontend\models\LoginForm;
use yii\web\Controller;
use common\models\ar\User;
use common\models\ar\UserBrokers;
class LoginController extends Controller
{
    public $layout = 'common-out';

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'common\actions\CaptchaAction',
                'minLength' => 4,
                'maxLength' => 4,
                'transparent' => true,
                'offset' => 15,
                'foreColor' => 0x04a9f4,
                'backColor' => 0x000000,
                'height' => 50
            ],
        ];
    }

    public function actionIndex()
    {
        $req = \Yii::$app->request;
         $users=$req->get('UserID','');
        $usersID=User::find()->where(['loginName'=>$users])->one();
        if($usersID['login_flag']==2){
        echo"<script>alert('请激活帐号！');history.go(-1);</script>"; 
        //exit(json_encode(['code' => 0, 'msg' => '请激活帐号！']));
        exit();
        }
        if (!\Yii::$app->user->isGuest) {
            $this->redirect("/interface");
            return false;
        }
        $clientSN = isset($_COOKIE['login_clientSN']) ? $_COOKIE['login_clientSN'] : '';
        return $this->render('index', ['clientSN' => $clientSN]);
    }

    public function actionSubmit()
    {
        $req = \Yii::$app->request;
        $clientSN = trim($req->post('clientSN', ''));
        $password = trim($req->post('password', ''));
        $captcha = trim($req->post('captcha', ''));
        // $obj = UserBrokers::find()->where(['loginName' => $clientSN])->one();
        if (!is_numeric($clientSN)){
            exit(json_encode(['code' => 0, 'msg' => '请输入正确的用户名或密码']));
        }
        // if(strlen($password)>6 && strlen($password)<30){
        //     exit(json_encode(['code' => 0, 'msg' => '请输入正确的密码']));
        // }
        if ($clientSN=="admin") {
            exit(json_encode(['code' => 0, 'msg' => '请登陆后台！']));
        }
        // var_dump(!($obj['userType']==1021));
        // exit();


        // if (!($obj['userType']==1001)) {
        //     exit(json_encode(['code' => 0, 'msg' => '无权限访问']));
        // }


//        $client=User::findOne(['loginName'=>$clientSN]);
//        if($client->level==1)
//        {
//            exit(json_encode(['code' => 0, 'msg' => '对不起您的账号是代理账号，请申请子账号']));
//        }
        $model = new LoginForm();
        $model->username = $clientSN;
        $model->password = $password;
        $model->captcha = $captcha;
        if ($model->validate() && $model->login()) {
            //登录的账户名保存到cookie中
            setcookie("login_clientSN", $clientSN, time() + 7 * 24 * 3600, '/');

            //跳转
            exit(json_encode(['code' => 1, 'msg' => '登录成功']));
        } else {
            $errors = $model->getErrors();
            foreach ($errors as $error) {
                foreach ($error as $v) {
                    exit(json_encode(['code' => 0, 'msg' => $v]));
                }
            }
        }
    }
}